package com.ruoyi.demo.controller;

import java.util.List;

import com.ruoyi.demo.domain.TbUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.demo.service.ITbUserService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 一对多-用户Controller
 *
 * @author ruoyi
 * @date 2020-05-23
 */
@Controller
@RequestMapping("/system/tbuser")
public class TbUserController extends BaseController
{
    private String prefix = "demo/tbuser";

    @Autowired
    private ITbUserService tbUserService;

    @RequiresPermissions("system:tbuser:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/user";
    }

    /**
     * 查询一对多-用户列表
     */
    @RequiresPermissions("system:tbuser:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbUser tbUser)
    {
        startPage();
        List<TbUser> list = tbUserService.selectTbUserList(tbUser);
        return getDataTable(list);
    }

    /**
     * 导出一对多-用户列表
     */
    @RequiresPermissions("system:tbuser:export")
    @Log(title = "一对多-用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbUser tbUser)
    {
        List<TbUser> list = tbUserService.selectTbUserList(tbUser);
        ExcelUtil<TbUser> util = new ExcelUtil<TbUser>(TbUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 新增一对多-用户
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存一对多-用户
     */
    @RequiresPermissions("system:tbuser:add")
    @Log(title = "一对多-用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TbUser tbUser)
    {
        return toAjax(tbUserService.insertTbUser(tbUser));
    }

    /**
     * 修改一对多-用户
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TbUser tbUser = tbUserService.selectTbUserById(id);
        mmap.put("tbuser", tbUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存一对多-用户
     */
    @RequiresPermissions("system:tbuser:edit")
    @Log(title = "一对多-用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TbUser tbUser)
    {
        return toAjax(tbUserService.updateTbUser(tbUser));
    }

    /**
     * 删除一对多-用户
     */
    @RequiresPermissions("system:tbuser:remove")
    @Log(title = "一对多-用户", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tbUserService.deleteTbUserByIds(ids));
    }
}
